﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class TruckManage_DriverEdit : System.Web.UI.Page
{
    IBNDriverHandler driverManager = new BNDriverHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1043") || strPermission.Contains("1044")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }           

            dataBind();
            setView();
        }
    }
    private void dataBind()
    {

        if (Request["PKID"] != null)
        {
            BN_DriverQuery query = driverManager.GetDriverByID(int.Parse(Request["PKID"]));
            if (query != null)
            {
                txtCode.Text = query.Code;
                txtName.Text = query.Name;
                if (query.Sex == 0)
                {
                    rad0.Checked = true;
                    rad1.Checked = false;
                }
                else
                {
                    rad0.Checked = false;
                    rad1.Checked = true;
                }
                txtIDCard.Text = query.IDCard;
                txtAddress.Text = query.Address;
                txtMPhone.Text = query.MPhone;
                txtLicence.Text = query.Licence;
                txtDrivingAge.Text = query.DrivingAge;
                if (query.YCDate != System.DateTime.MinValue)
                {
                    txtYCDate.Text = query.YCDate.ToString("yyyy-MM-dd");
                }
                if (query.Bail != System.Int32.MinValue)
                {
                    txtBail.Text = query.Bail.ToString();
                }
                txtRemark.Text = query.Remark;
                if (query.Status ==0)
                {
                    radNo.Checked = true;
                    radYes.Checked = false;
                }
                else
                {
                    radNo.Checked = false;
                    radYes.Checked = true;
                }

            }
        }
    }

    private void setView()
    {
        if (Request["PKID"] != null)
        {
            txtCode.Enabled = false;
        }
        else
        {
            txtCode.Enabled = true;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BN_DriverQuery driverQuery = new BN_DriverQuery();

        driverQuery.Name = txtName.Text.Trim();
        if (rad0.Checked)
        {
            driverQuery.Sex = 0;
        }
        else
        {
            driverQuery.Sex = 1;
        }
        driverQuery.IDCard = txtIDCard.Text;
        driverQuery.Address = txtAddress.Text;
        driverQuery.MPhone = txtMPhone.Text;
        driverQuery.Licence = txtLicence.Text;
        driverQuery.DrivingAge = txtDrivingAge.Text;
        if (txtYCDate.Text != null && txtYCDate.Text.Trim() != "")
        {
            driverQuery.YCDate = DateTime.Parse(txtYCDate.Text);
        }
        if (txtBail.Text != null && txtBail.Text.Trim() != "")
        {
            driverQuery.Bail = int.Parse(txtBail.Text.Trim());
        }
        driverQuery.Remark = txtRemark.Text;
        if (radYes.Checked)
        {
            driverQuery.Status = 1;
        }
        else
        {
            driverQuery.Status = 0;
        }
        driverQuery.ClientID = int.Parse(Session["LoginUserClientID"].ToString());
        driverQuery.LastModUser = int.Parse(Session["UserID"].ToString());
        driverQuery.LastModDate = DateTime.Now;

        try
        {
            if (Request["PKID"] != null)
            {
                BN_DriverQuery tmp = driverManager.GetDriverByID(int.Parse(Request["PKID"]));
                if (tmp == null)
                {
                    Jscript.Alert("找不到该记录，更新失败!", this.Page);
                }
                else
                {
                    driverQuery.PKID = tmp.PKID;
                    driverQuery.Code = tmp.Code;
                    driverQuery.IsFree = tmp.IsFree;
                    if(driverManager.UpdateDriver(driverQuery))
                    {
                        Jscript.AlertAndRedirect("更新司机资料成功", "DriverList.aspx", this.Page);
                    }
                    else
                    {
                        Jscript.Alert("更新司机资料失败!", this.Page);
                    }
                }
            }
            else
            {
                driverQuery.Code = txtCode.Text.Trim();
                driverQuery.IsFree = 1;
                if (driverManager.AddDriver(driverQuery))
                {
                    Jscript.AlertAndRedirect("添加司机资料成功", "DriverList.aspx", this.Page);
                }
                else
                {
                    Jscript.Alert("添加司机资料失败!", this.Page);
                }
            }
        }
        catch
        {
            Jscript.Alert("保存司机资料失败!", this.Page);
        }
        
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("DriverList.aspx");
    }
}
